package com.plter.minios
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	
	[Event(name="complete",type="flash.events.Event")]
	[Event(name="init",type="flash.events.Event")]
	[Event(name="open",type="flash.events.Event")]
	[Event(name="unload",type="flash.events.Event")]
	[Event(name="ioError",type="flash.events.IOErrorEvent")]
	[Event(name="httpStatus",type="flash.events.HTTPStatusEvent")]
	[Event(name="progress",type="flash.events.ProgressEvent")]
	
	/**
	 * 用于加载动态链接库swf文件
	 * @author Jaja
	 */
	public final class DllLoader extends EventDispatcher
	{
		private var _loader:Loader;
		
		public function DllLoader()
		{
			_loader=new Loader;
			
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,eventHandler);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,eventHandler);
			_loader.contentLoaderInfo.addEventListener(Event.INIT,eventHandler);
			_loader.contentLoaderInfo.addEventListener(Event.OPEN,eventHandler);
			_loader.contentLoaderInfo.addEventListener(Event.UNLOAD,eventHandler);
			_loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS,eventHandler);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,eventHandler);
		}
		
		/**
		 * 获得此对象对应的Loader对象
		 */
		public function get loader():Loader{
			return _loader;
		}
		
		public function unload():void{
			loader.unload();
		}
		
		public function unloadAndStop(gc:Boolean=true):void{
			loader.unloadAndStop(gc);
		}
		
		/**
		 * 从所加载的库文件中获得一个定义
		 * @param	name	将要获得的定义的全名
		 */
		public function getClass(name:String):Class{
			return loader.contentLoaderInfo.applicationDomain.getDefinition(name) as Class;
		}
		
		/**
		 * 加载一个库文件,swf格式
		 * @param url	库文件的路径
		 */
		public function load(url:String):void{
			_loader.load(new URLRequest(url));
		}
		
		private function eventHandler(event:Event):void{
			dispatchEvent(event);
		}
		
	}
}